setwd("C:/Users/Daniel de Kadt/Dropbox (Personal)/Projects/SA_cohort_effects/replication")
library(foreign)
library(ggplot2)
library(grid)
library(plyr)

d = read.dta("sasas_ddk12.dta")
c = d[,c("race","force","vote","time")]
c = na.omit(c)
colnames(c)[2] = "age"
c$count = 1

c = c[which(c$race=="african/black"),]

votes = ddply(c,~age,summarise,mean=mean(vote),sd=sd(vote),count=sum(count))
votes$treat = ifelse(votes$age<0,0,1)
votes$se = votes$sd/sqrt(votes$count)
votes$ci = (1.96*(votes$sd/sqrt(votes$count)))

###Figure 1 In Paper
main <- 
ggplot(votes[which(votes$age>-11),], aes(x = age, y = mean)) + #This line gives your data (na.omit(a)), then defines x and y. 
  geom_rect(aes(ymin=0.78,ymax=.92,xmin=-4.5,xmax=4.5), inherit.aes = FALSE, alpha=.9, fill=NA, colour="grey") +  
  geom_point(aes(colour = factor(treat)), size=3) +
  scale_colour_discrete(name="Treatment?")      + 
  geom_vline(x = 0, size = 1) + 
  scale_x_continuous("Years from 18 in 1994", limits=c(-11, 70)) + 
  scale_y_continuous("Did you vote in the last election (1 year age bin means)?",limits=c(0.4,1)) +
  theme_bw() + 
  theme(axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.title.x=element_text(size=12),
        axis.title.y=element_text(size=12,angle=90))

sub <- ggplotGrob(ggplot(votes[which(votes$age>-5 & votes$age<5),], aes(x = age, y = mean)) + #This line gives your data (na.omit(a)), then defines x and y. 
                    geom_point(aes(colour = factor(treat)), size=3,guide=FALSE) +
                    #geom_errorbar(aes(ymin=mean-se, ymax=mean+se,colour = factor(treat)), alpha=.5, width=0, guide=FALSE) +
                    scale_colour_discrete(name="Treatment?", guide=FALSE)      +
                    geom_vline(x = 0, size = 1) + scale_x_continuous("") + 
                    scale_y_continuous("",limits=c(0.78,.92)) +
                    ggtitle("Inset: Close-Up of Cutpoint (Rectangle)") +
                    theme_bw() + 
                    theme(axis.line = element_line(colour = "black"),
                          plot.title= element_text(size=10),
                          panel.border = element_blank(),
                          panel.background = element_blank(),
                          axis.title.x=element_text(size=10),
                          axis.title.y=element_text(size=10,angle=90)))

pdf(file="force_means_plot.pdf",height=6,width=8)
main + annotation_custom(grob = sub, xmin = 15, xmax = 55, ymin = 0.4, ymax = 0.7)
dev.off()

### Whites - Appendix
c = d[,c("race","force","vote","time")]
c = na.omit(c)
colnames(c)[2] = "age"
c$count = 1

c = c[which(c$race=="white"),]

votes = ddply(c,~age,summarise,mean=mean(vote),sd=sd(vote),count=sum(count))
votes$treat = ifelse(votes$age<0,0,1)
votes$se = votes$sd/sqrt(votes$count)
votes$ci = (1.96*(votes$sd/sqrt(votes$count)))

main <- 
  ggplot(votes[which(votes$age>-11),], aes(x = age, y = mean)) + #This line gives your data (na.omit(a)), then defines x and y. 
  geom_rect(aes(ymin=0.5,ymax=.85,xmin=-4.5,xmax=4.5), inherit.aes = FALSE, alpha=.9, fill=NA, colour="grey") +  
  geom_point(aes(colour = factor(treat)), size=3) +
  scale_colour_discrete(name="Treatment?")      + 
  geom_vline(x = 0, size = 1) +
  scale_x_continuous("Years from 18 in 1994", limits=c(-11, 70)) + 
  scale_y_continuous("Did you vote in the last election (1 year age bin means)?",limits=c(0.4,1)) +
  theme_bw() + 
  theme(axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.title.x=element_text(size=12),
        axis.title.y=element_text(size=12,angle=90))

sub <- ggplotGrob(ggplot(votes[which(votes$age>-5 & votes$age<5),], aes(x = age, y = mean)) + #This line gives your data (na.omit(a)), then defines x and y. 
                    geom_point(aes(colour = factor(treat)), size=3,guide=FALSE) +
                    #geom_errorbar(aes(ymin=mean-se, ymax=mean+se,colour = factor(treat)), alpha=.5, width=0, guide=FALSE) +
                    scale_colour_discrete(name="Treatment?", guide=FALSE)      +
                    geom_vline(x = 0, size = 1) + scale_x_continuous("") + 
                    scale_y_continuous("",limits=c(0.5,.85)) +
                    ggtitle("Inset: Close-Up of Cutpoint (Rectangle)") +
                    theme_bw() + 
                    theme(axis.line = element_line(colour = "black"),
                          plot.title= element_text(size=10),
                          panel.border = element_blank(),
                          panel.background = element_blank(),
                          axis.title.x=element_text(size=10),
                          axis.title.y=element_text(size=10,angle=90)))


pdf(file="force_means_plot_white.pdf",height=6,width=8)
main + annotation_custom(grob = sub, xmin = 15, xmax = 55, ymin = 0.35, ymax = 0.65)
dev.off()

### Coloureds - Appendix
c = d[,c("race","force","vote","time")]
c = na.omit(c)
colnames(c)[2] = "age"
c$count = 1

c = c[which(c$race=="coloured"),]

votes = ddply(c,~age,summarise,mean=mean(vote),sd=sd(vote),count=sum(count))
votes$treat = ifelse(votes$age<0,0,1)
votes$se = votes$sd/sqrt(votes$count)
votes$ci = (1.96*(votes$sd/sqrt(votes$count)))

main <- 
  ggplot(votes[which(votes$age>-11),], aes(x = age, y = mean)) + #This line gives your data (na.omit(a)), then defines x and y. 
  geom_rect(aes(ymin=0.5,ymax=.85,xmin=-4.5,xmax=4.5), inherit.aes = FALSE, alpha=.9, fill=NA, colour="grey") +  
  geom_point(aes(colour = factor(treat)), size=3) +
  scale_colour_discrete(name="Treatment?")      + 
  geom_vline(x = 0, size = 1) + 
  scale_x_continuous("Years from 18 in 1994", limits=c(-11, 70)) + 
  scale_y_continuous("Did you vote in the last election (1 year age bin means)?",limits=c(0.4,1)) +
  theme_bw() + 
  theme(axis.line = element_line(colour = "black"),
        panel.border = element_blank(),
        panel.background = element_blank(),
        axis.title.x=element_text(size=12),
        axis.title.y=element_text(size=12,angle=90))

sub <- ggplotGrob(ggplot(votes[which(votes$age>-5 & votes$age<5),], aes(x = age, y = mean)) + #This line gives your data (na.omit(a)), then defines x and y. 
                    geom_point(aes(colour = factor(treat)), size=3,guide=FALSE) +
                    #geom_errorbar(aes(ymin=mean-se, ymax=mean+se,colour = factor(treat)), alpha=.5, width=0, guide=FALSE) +
                    scale_colour_discrete(name="Treatment?", guide=FALSE)      +
                    geom_vline(x = 0, size = 1) + scale_x_continuous("") + 
                    scale_y_continuous("",limits=c(0.5,.85)) +
                    ggtitle("Inset: Close-Up of Cutpoint (Rectangle)") +
                    theme_bw() + 
                    theme(axis.line = element_line(colour = "black"),
                          plot.title= element_text(size=10),
                          panel.border = element_blank(),
                          panel.background = element_blank(),
                          axis.title.x=element_text(size=10),
                          axis.title.y=element_text(size=10,angle=90)))

pdf(file="force_means_plot_coloured.pdf",height=6,width=8)
main + annotation_custom(grob = sub, xmin = 15, xmax = 55, ymin = 0.4, ymax = 0.7)
dev.off()

